<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>种子单元识别代码追溯平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
            color: white;
            padding: 30px;
            text-align: center;
            position: relative;
        }

        .header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="20" cy="20" r="2" fill="rgba(255,255,255,0.1)"/><circle cx="80" cy="40" r="3" fill="rgba(255,255,255,0.1)"/><circle cx="40" cy="80" r="1" fill="rgba(255,255,255,0.1)"/></svg>');
        }

        .header h1 {
            font-size: 28px;
            margin-bottom: 10px;
            position: relative;
            z-index: 1;
        }

        .header p {
            font-size: 16px;
            opacity: 0.9;
            position: relative;
            z-index: 1;
        }

        /* 搜索区域样式 */
        .search-section {
            padding: 30px;
            background: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
        }

        .search-container {
            display: flex;
            gap: 15px;
            max-width: 600px;
            margin: 0 auto;
        }

        .search-input {
            flex: 1;
            padding: 12px 15px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        .search-input:focus {
            outline: none;
            border-color: #4CAF50;
        }

        .search-btn {
            padding: 12px 24px;
            background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s ease;
        }

        .search-btn:hover {
            transform: translateY(-2px);
        }

        .search-btn:active {
            transform: translateY(0);
        }

        .loading {
            text-align: center;
            padding: 50px;
            color: #666;
        }

        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #4CAF50;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px;
        }

        @keyframes spin {
            0% {
                transform: rotate(0deg);
            }
            100% {
                transform: rotate(360deg);
            }
        }

        .content {
            padding: 30px;
        }

        .section {
            margin-bottom: 30px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
        }

        .section-header {
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
            padding: 15px 20px;
            border-bottom: 3px solid #4CAF50;
            font-weight: bold;
            font-size: 18px;
            color: #333;
            display: flex;
            align-items: center;
        }

        .section-header::before {
            content: '●';
            color: #4CAF50;
            margin-right: 10px;
            font-size: 20px;
        }

        .section-content {
            padding: 20px;
            background: #fff;
        }

        .info-item {
            display: flex;
            padding: 12px 0;
            border-bottom: 1px solid #f0f0f0;
            transition: background-color 0.3s ease;
        }

        .info-item:hover {
            background-color: #f8f9fa;
        }

        .info-item:last-child {
            border-bottom: none;
        }

        .info-label {
            font-weight: bold;
            color: #333;
            min-width: 120px;
            position: relative;
        }

        .info-label::after {
            content: ':';
            margin-left: 5px;
        }

        .info-value {
            flex: 1;
            color: #555;
            padding-left: 15px;
        }

        .info-value a {
            color: #4CAF50;
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .info-value a:hover {
            color: #45a049;
            text-decoration: underline;
        }

        /* 大码列表样式 */
        .big-code-list {
            background: #fff;
        }

        .big-code-item {
            display: flex;
            align-items: center;
            padding: 15px;
            border-bottom: 1px solid #f0f0f0;
            transition: background-color 0.3s ease;
            gap: 20px;
        }

        .big-code-item:hover {
            background-color: #f8f9fa;
        }

        .big-code-item:last-child {
            border-bottom: none;
        }

        .big-code-field {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 5px;
        }

        .big-code-label {
            font-weight: bold;
            color: #333;
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .big-code-value {
            color: #555;
            font-size: 14px;
        }

        .big-code-value.highlight {
            font-weight: bold;
            color: #333;
        }

        .big-code-value.code {
            background: #e3f2fd;
            color: #1976d2;
            padding: 4px 8px;
            border-radius: 4px;
            font-family: monospace;
            font-size: 12px;
            display: inline-block;
        }

        .big-code-value a {
            color: #4CAF50;
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .big-code-value a:hover {
            color: #45a049;
            text-decoration: underline;
        }

        .product-intro {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            border-left: 4px solid #4CAF50;
            margin-top: 10px;
            line-height: 1.6;
        }

        .error {
            text-align: center;
            padding: 50px;
            color: #e74c3c;
        }

        .error-icon {
            font-size: 48px;
            margin-bottom: 20px;
        }

        .qr-code {
            text-align: center;
            margin: 20px 0;
        }

        .qr-code img {
            max-width: 150px;
            border-radius: 8px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .footer {
            background: #f8f9fa;
            padding: 20px;
            text-align: center;
            color: #666;
            font-size: 14px;
            border-top: 1px solid #e9ecef;
        }

        @media (max-width: 768px) {
            .container {
                margin: 10px;
                border-radius: 10px;
            }

            .header {
                padding: 20px;
            }

            .header h1 {
                font-size: 24px;
            }

            .content {
                padding: 20px;
            }

            .search-container {
                flex-direction: column;
            }

            .search-btn {
                width: 100%;
            }

            .info-item {
                flex-direction: column;
            }

            .info-label {
                min-width: auto;
                margin-bottom: 5px;
            }

            .info-value {
                padding-left: 0;
            }

                         .big-code-item {
                 flex-direction: column;
                 align-items: flex-start;
                 gap: 15px;
             }

             .big-code-field {
                 width: 100%;
             }
        }

        .highlight {
            background: linear-gradient(120deg, #a8edea 0%, #fed6e3 100%);
            padding: 2px 6px;
            border-radius: 4px;
            font-weight: bold;
        }

        .badge {
            background: #4CAF50;
            color: white;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }

        .no-results {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .no-results-icon {
            font-size: 48px;
            margin-bottom: 20px;
            opacity: 0.5;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>种子单元识别代码追溯平台</h1>
        <p>Seed Unit Identification Code Traceability Platform</p>
    </div>

    <!-- 搜索区域 -->
    <div class="search-section">
        <div class="search-container">
            <input type="text" id="searchInput" class="search-input" placeholder="请输入种子单元识别代码"/>
            <button id="searchBtn" class="search-btn">搜索</button>
        </div>
    </div>

    <div id="loading" class="loading" style="display: none;">
        <div class="spinner"></div>
        <p>正在加载种子信息...</p>
    </div>

    <!-- 小码详细信息 -->
    <div id="smallCodeContent" class="content" style="display: none;">
        <!-- 产品信息 -->
        <div class="section">
            <div class="section-header">产品信息</div>
            <div class="section-content">
                <div class="info-item">
                    <div class="info-label">品种名称</div>
                    <div class="info-value highlight" id="productName">-</div>
                </div>
                <div class="info-item">
                    <div class="info-label">生产经营者名称</div>
                    <div class="info-value" id="company">-</div>
                </div>
                <div class="info-item">
                    <div class="info-label">单元识别代码</div>
                    <div class="info-value" id="companyCode">-</div>
                </div>
                <div class="info-item">
                    <div class="info-label">追溯网址</div>
                    <div class="info-value" id="zzUrl">-</div>
                </div>
            </div>
        </div>

        <!-- 生产信息 -->
        <div class="section">
            <div class="section-header">生产信息</div>
            <div class="section-content">
                <div class="info-item">
                    <div id="productAttrs"></div>
                </div>
            </div>
        </div>

        <!-- 销售信息 -->
        <div class="section">
            <div class="section-header">销售信息</div>
            <div class="section-content">
                <div id="orderInfo">
                    <!-- 销售信息将在这里动态显示 -->
                </div>
            </div>
        </div>
    </div>

    <!-- 大码列表 -->
    <div id="bigCodeContent" class="content" style="display: none;">
        <div class="section">
            <div class="section-header">种子单元列表</div>
            <div class="section-content">
                <div id="bigCodeList" class="big-code-list">
                    <!-- 大码列表将在这里动态显示 -->
                </div>
            </div>
        </div>
    </div>

    <div id="error" class="error" style="display: none;">
        <div class="error-icon">⚠️</div>
        <h3>获取信息失败</h3>
        <p id="errorMsg">请检查网络连接或稍后重试</p>
    </div>

    <div id="noResults" class="no-results" style="display: none;">
        <div class="no-results-icon">🔍</div>
        <h3>未找到相关信息</h3>
        <p>请检查输入的种子单元识别代码是否正确</p>
    </div>

    <div class="footer">
        <p>© 2024 种子追溯平台 | 确保种子质量，保障农业安全</p>
    </div>
</div>

<script>
    // 获取URL参数
    function getUrlParameter(name) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(name);
    }

    // 隐藏所有内容区域
    function hideAllContent() {
        document.getElementById('loading').style.display = 'none';
        document.getElementById('smallCodeContent').style.display = 'none';
        document.getElementById('bigCodeContent').style.display = 'none';
        document.getElementById('error').style.display = 'none';
        document.getElementById('noResults').style.display = 'none';
    }

    // 显示加载状态
    function showLoading() {
        hideAllContent();
        document.getElementById('loading').style.display = 'block';
    }

    // 显示错误信息
    function showError(message) {
        hideAllContent();
        document.getElementById('error').style.display = 'block';
        document.getElementById('errorMsg').textContent = message;
    }

    // 显示无结果
    function showNoResults() {
        hideAllContent();
        document.getElementById('noResults').style.display = 'block';
    }

    // 显示小码内容
    function showSmallCodeContent() {
        hideAllContent();
        document.getElementById('smallCodeContent').style.display = 'block';
    }

    // 显示大码内容
    function showBigCodeContent() {
        hideAllContent();
        document.getElementById('bigCodeContent').style.display = 'block';
    }

    // 搜索种子信息
    async function searchSeedInfo(code) {
        if (!code || code.trim() === '') {
            showError('请输入种子单元识别代码');
            return;
        }

        console.log('开始搜索种子信息，code:', code);
        showLoading();

        try {
            const apiUrl = `https://zz.scjat.cn/api/seed/getInfo?code=${code}`;
            // console.log('请求URL:', apiUrl);
            //
            const response = await fetch(apiUrl, {
                method: 'GET',
                mode: 'cors',
                headers: {
                    'Content-Type': 'application/json',
                }
            });

            var result = await response.json();
            console.log('接口返回数据:', result);
            // var  result = {
            //     "status": 200,
            //     "msg": "success",
            //     "data": {
            //         "type": "small",
            //         "data": {
            //             "base": {
            //                 "name": "成豆13",
            //                 "seed_id": 1018,
            //                 "code": "11010185232604105329",
            //                 "company": "开发调试用测试公司",
            //                 "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105329"
            //             },
            //             "attr": [],
            //             "order": [{
            //                 "add_time": "2025-07-12",
            //                 "company": "爆发国际公司",
            //                 "company_short": "爆发国际",
            //                 "user_name": "李总",
            //                 "user_phone": "18711111111",
            //                 "area_name": "黑龙江省 佳木斯市 桦川县"
            //             }]
            //         }
            //     }
            // };

            // var result = {
            //     "status":
            //         200, "msg":
            //         "success", "data":
            //         {
            //             "type":
            //                 "big", "data":
            //                 [{
            //                     "code": "11010185232604105329",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105329"
            //                 }, {
            //                     "code": "11010185232604105330",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105330"
            //                 }, {
            //                     "code": "11010185232604105331",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105331"
            //                 }, {
            //                     "code": "11010185232604105332",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105332"
            //                 }, {
            //                     "code": "11010185232604105336",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105336"
            //                 }, {
            //                     "code": "11010185232604105339",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105339"
            //                 }, {
            //                     "code": "11010185232604105341",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105341"
            //                 }, {
            //                     "code": "11010185232604105342",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105342"
            //                 }, {
            //                     "code": "11010185232604105345",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105345"
            //                 }, {
            //                     "code": "11010185232604105346",
            //                     "seed_name": "成豆13",
            //                     "company": "开发调试用测试公司",
            //                     "origin_url": "https:\/\/zz.scjat.cn\/seed-info.html?code=11010185232604105346"
            //                 }]
            //         }
            // };
            if (result.status === 200 && result.data) {
                const {type, data} = result.data;

                if (type === 'big') {
                    // 大码数据
                    displayBigCodeList(data);
                    showBigCodeContent();
                } else if (type === 'small') {
                    // 小码数据
                    displaySmallCodeInfo(data, code);
                    showSmallCodeContent();
                } else {
                    showError('未知的数据类型');
                }
            } else {
                showError(result.msg || '获取数据失败');
            }

        } catch (error) {
            // console.error('请求失败详情:', error);
            showError(`网络请求失败: ${error.message}`);
        }
    }

         // 显示大码列表
     function displayBigCodeList(data) {
         const listContainer = document.getElementById('bigCodeList');
         listContainer.innerHTML = '';

         if (!data || data.length === 0) {
             listContainer.innerHTML = '<div class="no-results"><p>暂无数据</p></div>';
             return;
         }

         data.forEach(item => {
             const itemDiv = document.createElement('div');
             itemDiv.className = 'big-code-item';
             itemDiv.innerHTML = `
                 <div class="big-code-field">
                     <div class="big-code-label">品种名称</div>
                     <div class="big-code-value highlight">${item.seed_name || '-'}</div>
                 </div>
                 <div class="big-code-field">
                     <div class="big-code-label">生产经营者名称</div>
                     <div class="big-code-value">${item.company || '-'}</div>
                 </div>
                 <div class="big-code-field">
                     <div class="big-code-label">单元识别代码</div>
                     <div class="big-code-value code">${item.code || '-'}</div>
                 </div>
                 <div class="big-code-field">
                     <div class="big-code-label">追溯网址</div>
                     <div class="big-code-value">
                         <a href="${item.origin_url || '#'}" target="_blank">${item.origin_url || '-'}</a>
                     </div>
                 </div>
             `;
             listContainer.appendChild(itemDiv);
         });
     }

    // 显示小码详细信息
    function displaySmallCodeInfo(data, code) {
        const {base, attr, order} = data;

        // 基础信息
        if (base) {
            document.getElementById('productName').textContent = base.name || '-';
            document.getElementById('company').textContent = base.company || '-';
            document.getElementById('companyCode').textContent = code;

            // 追溯网址支持超链接
            const zzUrlElement = document.getElementById('zzUrl');
            if (base.origin_url) {
                zzUrlElement.innerHTML = `<a href="${base.origin_url}" target="_blank">${base.origin_url}</a>`;
            } else {
                zzUrlElement.textContent = '-';
            }
        }

        // 属性信息
        if (attr && attr.length > 0) {
            const attrsContainer = document.getElementById('productAttrs');
            attrsContainer.innerHTML = '';
            attr.forEach(item => {
                const attrDiv = document.createElement('div');
                attrDiv.className = 'info-item';
                attrDiv.innerHTML = `
                    <div class="info-label">${item.name}</div>
                    <div class="info-value">${item.val}</div>
                `;
                attrsContainer.appendChild(attrDiv);
            });
        } else {
            document.getElementById('productAttrs').innerHTML = `
                <div class="info-item">
                    <div class="info-label">生产信息</div>
                    <div class="info-value">暂无生产信息</div>
                </div>
            `;
        }

        // 销售信息（订单信息）
        if (order && Array.isArray(order) && order.length > 0) {
            const orderContainer = document.getElementById('orderInfo');
            orderContainer.innerHTML = '';

            order.forEach((orderItem, index) => {
                // 出库日期
                if (orderItem.add_time) {
                    const dateStr = orderItem.add_time;
                    const [year, month, day] = dateStr.split('-');
                    const formattedDate = `${year}年${parseInt(month)}月${parseInt(day)}日`;

                    const addTimeDiv = document.createElement('div');
                    addTimeDiv.className = 'info-item';
                    addTimeDiv.innerHTML = `
                        <div class="info-label">出库日期</div>
                        <div class="info-value">${formattedDate}</div>
                    `;
                    orderContainer.appendChild(addTimeDiv);
                }

                // 发往地址
                if (orderItem.area_name) {
                    const areaDiv = document.createElement('div');
                    areaDiv.className = 'info-item';
                    areaDiv.innerHTML = `
                        <div class="info-label">发往地址</div>
                        <div class="info-value">${orderItem.area_name}</div>
                    `;
                    orderContainer.appendChild(areaDiv);
                }

                // 经销商
                if (orderItem.company) {
                    const companyDiv = document.createElement('div');
                    companyDiv.className = 'info-item';
                    companyDiv.innerHTML = `
                        <div class="info-label">经销商</div>
                        <div class="info-value">${orderItem.company}</div>
                    `;
                    orderContainer.appendChild(companyDiv);
                }

                // 多个订单之间的分隔线
                if (order.length > 1 && index < order.length - 1) {
                    const separator = document.createElement('div');
                    separator.style.borderTop = '1px solid #e9ecef';
                    separator.style.margin = '20px 0';
                    orderContainer.appendChild(separator);
                }
            });
        } else {
            const orderContainer = document.getElementById('orderInfo');
            orderContainer.innerHTML = `
                <div class="info-item">
                    <div class="info-label">销售信息</div>
                    <div class="info-value">暂无销售信息</div>
                </div>
            `;
        }
    }

    // 绑定搜索事件
    document.getElementById('searchBtn').addEventListener('click', function () {
        const code = document.getElementById('searchInput').value.trim();
        searchSeedInfo(code);
    });

    // 绑定回车键搜索
    document.getElementById('searchInput').addEventListener('keypress', function (e) {
        if (e.key === 'Enter') {
            const code = this.value.trim();
            searchSeedInfo(code);
        }
    });

    // 页面加载完成后检查URL参数
    document.addEventListener('DOMContentLoaded', function () {
        console.log('页面DOM加载完成');
        const code = getUrlParameter('code');
        if (code) {
            document.getElementById('searchInput').value = code;
            searchSeedInfo(code);
        }
    });
</script>
</body>
</html> 